//
//  RCRecommendFoodCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-22.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCRecommendFoodNewCell.h"
#import "NSString+NSString_Ex_.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "UIImageView+WebCache.h"
#import "RCRecommendFoodListDataModel.h"

#define kDefaultimageFile        @"image_recommendFood_image_bg.png"
#define kStartActiveImageFile    @"image_start_active.png"
#define kStartInActiveImageFile  @"image_merchant_inactive.png"
#define kMapImageFile            @"image_allCity_district.png"
#define kLineImageFile           @"image_line_type3.png"
#define kBGImageFile             @"image_discount_cell_bg.png"

@interface RCRecommendFoodNewCell()

@property(nonatomic,retain)UIImageView *defaultImageView;
@property(nonatomic,retain)UIImageView *picImageView;
@property(nonatomic,retain)UILabel     *foodNameLabel;
@property(nonatomic,retain)UIImageView *startImageView1;
@property(nonatomic,retain)UIImageView *startImageView2;
@property(nonatomic,retain)UIImageView *startImageView3;
@property(nonatomic,retain)UIImageView *startImageView4;
@property(nonatomic,retain)UIImageView *startImageView5;
@property(nonatomic,retain)UILabel     *descLabel;
@property(nonatomic,retain)UILabel     *addressLabel;
@property(nonatomic,retain)UILabel     *priceLabel;
@property(nonatomic,retain)UIImageView *mapImageView;
@property(nonatomic,retain)UILabel     *distanceLabel;
@property(nonatomic,retain)UIImageView *lineImageView;

@end

@implementation RCRecommendFoodNewCell

@synthesize defaultImageView = _defaultImageView;
@synthesize picImageView = _picImageView;
@synthesize startImageView1 = _startImageView1;
@synthesize startImageView2 = _startImageView2;
@synthesize startImageView3 = _startImageView3;
@synthesize startImageView4 = _startImageView4;
@synthesize startImageView5 = _startImageView5;
@synthesize foodNameLabel = _foodNameLabel;
@synthesize descLabel = _descLabel;
@synthesize addressLabel = _addressLabel;
@synthesize priceLabel = _priceLabel;
@synthesize mapImageView = _mapImageView;
@synthesize distanceLabel = _distanceLabel;
@synthesize lineImageView = _lineImageView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        [self setBackgroundColorWithImage:bgImage];
        self.backgroundColor = [UIColor clearColor];
        [self createPlaceHolderView];
    }
    return self;
}

-(void)dealloc{
    self.defaultImageView = nil;
    self.picImageView = nil;
    self.startImageView1 = nil;
    self.startImageView2 = nil;
    self.startImageView3 = nil;
    self.startImageView4 = nil;
    self.startImageView5 = nil;
    self.foodNameLabel = nil;
    self.descLabel = nil;
    self.addressLabel = nil;
    self.priceLabel = nil;
    self.mapImageView = nil;
    self.distanceLabel = nil;
    self.lineImageView = nil;
    [super dealloc];
}

-(void)createPlaceHolderView{
    _defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 14, 84, 66)];
    _defaultImageView.backgroundColor = [UIColor clearColor];
    UIImage *defaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDefaultimageFile ofType:nil]];
    defaultImage = [defaultImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [_defaultImageView setImage:defaultImage];
    [self addSubview:_defaultImageView];
    
    _picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 17, 78, 60)];
    _picImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_picImageView];
    
    _foodNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(108, 19, 200, 16)];
    _foodNameLabel.backgroundColor = [UIColor clearColor];
    _foodNameLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    _foodNameLabel.textColor = [UIColor colorWithHex:0xff505050];
    [self addSubview:_foodNameLabel];
    
    _startImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(108, 40, 14, 14)];
    _startImageView1.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView1];
    
    _startImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(123, 40, 14, 14)];
    _startImageView2.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView2];
    
    _startImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(138, 40, 14, 14)];
    _startImageView3.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView3];
    
    _startImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(153, 40, 14, 14)];
    _startImageView4.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView4];
    
    _startImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(168, 40, 14, 14)];
    _startImageView5.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView5];
    
    _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(185, 40, 200, 12)];
    _priceLabel.backgroundColor = [UIColor clearColor];
    _priceLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _priceLabel.textColor = [UIColor colorWithHex:0xff989797];
    [self addSubview:_priceLabel];
    
    _addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(108, 55, 145, 12)];
    _addressLabel.backgroundColor = [UIColor clearColor];
    _addressLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _addressLabel.textColor = [UIColor colorWithHex:0xff989797];
    [self addSubview:_addressLabel];
    
    
    
    _mapImageView = [[UIImageView alloc]initWithFrame:CGRectMake(255, 55, 8, 10)];
    _mapImageView.backgroundColor = [UIColor clearColor];
    [_mapImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kMapImageFile ofType:nil]]];
    [self addSubview:_mapImageView];
    
    _distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(266, 55, 50, 12)];
    _distanceLabel.backgroundColor = [UIColor clearColor];
    _distanceLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _distanceLabel.textColor = [UIColor colorWithHex:0xff989797];
    [self addSubview:_distanceLabel];
    
    _descLabel = [[UILabel alloc]initWithFrame:CGRectMake(108, 70, 200, 12)];
    _descLabel.backgroundColor = [UIColor clearColor];
    _descLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _descLabel.textColor = [UIColor colorWithHex:0xff989797];
    [self addSubview:_descLabel];
    
    _lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 93, 320, 1)];
    _lineImageView.backgroundColor = [UIColor clearColor];
    [_lineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]]];
    [self addSubview:_lineImageView];
    
    self.selectionStyle = UITableViewCellSelectionStyleGray;
}

-(void)bindData:(RCRecommendFoodListItem *)dataModel{
    if (nil == dataModel) {
        return;
    }
    if (![NSString stringIsNullOrEmpty:dataModel.foodPicUrl]) {
        [_picImageView setImageWithURL:[NSURL URLWithString:dataModel.foodPicUrl] placeholderImage:nil];
    }
    
    _foodNameLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.foodName]) {
        _foodNameLabel.hidden = NO;
        _foodNameLabel.text = dataModel.foodName;
    }
    
    NSInteger score = dataModel.foodScore;
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
    UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
    [_startImageView1 setImage:score > 0 ? activeImage : inactiveImage];
    [_startImageView2 setImage:score > 1 ? activeImage : inactiveImage];
    [_startImageView3 setImage:score > 2 ? activeImage : inactiveImage];
    [_startImageView4 setImage:score > 3 ? activeImage : inactiveImage];
    [_startImageView5 setImage:score > 4 ? activeImage : inactiveImage];
    
    _descLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.foodDesc]) {
        _descLabel.hidden = NO;
        _descLabel.text = [NSString stringWithFormat:@"推荐理由：%@",dataModel.foodDesc];
    }
    
    _addressLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.foodAddress]) {
        _addressLabel.hidden = NO;
        _addressLabel.text = [NSString stringWithFormat:@"地址：%@",dataModel.foodAddress];
    }
    
    _priceLabel.hidden = YES;
    if (dataModel.foodPrice > 0) {
        _priceLabel.hidden = NO;
        _priceLabel.text = [NSString stringWithFormat:@"价格：￥%d",dataModel.foodPrice];
    }
    
    _mapImageView.hidden = YES;
    _distanceLabel.hidden = YES;
    if (![NSString stringIsNullOrEmpty:dataModel.foodDistance]) {
        _mapImageView.hidden = NO;
        _distanceLabel.hidden = NO;
        _distanceLabel.text = dataModel.foodDistance;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
